package com.itboye.sunsun.person.ui;

import java.util.ArrayList;

import com.itboye.sunsun.R;
import com.itboye.sunsun.Application.App;
import com.itboye.sunsun.adapter.OrderViewPagerAdapter;
import com.itboye.sunsun.person.fragments.AllFragment;
import com.itboye.sunsun.person.fragments.PayFragment;
import com.itboye.sunsun.person.fragments.ReceiveFragment;
import com.itboye.sunsun.person.fragments.SendFragment;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.transition.Scene;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyOrderActivity extends FragmentActivity {

	private ViewPager mPager;
	private ArrayList<Fragment> fragmentList;
	private TextView view1, view2, view3, view4;
	private ImageView image;
	private int currIndex;// 当前页卡编号
	private float offset;// 图片移动的偏移量

	@Override
	protected void onCreate(Bundle savedInstance) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_myorder);
		System.out.println(">..");

		initTextView();
		initImage();
		initViewPager();
	}

	// 初始化标签
	public void initTextView() {
		view1 = (TextView) findViewById(R.id.tv_guid1);
		view2 = (TextView) findViewById(R.id.tv_guid2);
		view3 = (TextView) findViewById(R.id.tv_guid3);
		view4 = (TextView) findViewById(R.id.tv_guid4);

		view1.setOnClickListener(new txListener(0));
		view2.setOnClickListener(new txListener(1));
		view3.setOnClickListener(new txListener(2));
		view4.setOnClickListener(new txListener(3));
	}

	public class txListener implements OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mPager.setCurrentItem(index);
		}
	}

	/*
	 * 初始化图片的位移像素
	 */
	public void initImage() {
		image = (ImageView) findViewById(R.id.cursor);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int screenH = dm.heightPixels;
		offset = screenW / 4.0f;
		
		
		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams((int)offset-20, 2);
		image.setLayoutParams(params);


		Log.v("Hello-----screeW", "" + screenW);
		Log.v("Hello-----bmpW", "" + "" + screenH);

	}

	/*
	 * 初始化ViewPager
	 */
	public void initViewPager() {
		
		mPager = (ViewPager) findViewById(R.id.viewpager);
		fragmentList = new ArrayList<Fragment>();
		Fragment allFragment = new AllFragment(MyOrderActivity.this);
		Fragment payFragment = new PayFragment(MyOrderActivity.this);
		Fragment sendFragment = new SendFragment(MyOrderActivity.this);
		Fragment receiveFragment = new ReceiveFragment(MyOrderActivity.this);

		fragmentList.add(allFragment);
		fragmentList.add(payFragment);
		fragmentList.add(sendFragment);
		fragmentList.add(receiveFragment);

		Intent intent = getIntent();
		int i = intent.getIntExtra("fragmentCurrent", 0);
		currIndex = i;

		// //给ViewPager设置适配器
		mPager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));

		mPager.setCurrentItem(i);// 设置当前显示标签页为第一页
//		setTextColor(i + 1);
//
//		tranSlate(i, 0);
//
//		// Animation animation = new TranslateAnimation(currIndex * one, i
//		// * one, 0, 0);// 平移动画
//		// currIndex = i;
//		// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
//		// animation.setDuration(0);// 动画持续时间0.2秒
//		// image.startAnimation(animation);// 是用ImageView来显示动画的
//
//		mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {
		private float one = offset;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub

			// Toast.makeText(App.ctx, ""+arg0, 0).show();
			// Animation animation = new TranslateAnimation(currIndex * one,
			// arg0
			// * one, 0, 0);// 平移动画
			// currIndex = arg0;
			// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
			// animation.setDuration(200);// 动画持续时间0.2秒
			// image.startAnimation(animation);// 是用ImageView来显示动画的

			tranSlate(arg0, 200);
			currIndex = arg0;
			int i = currIndex + 1;
			setTextColor(i);

			// Toast.makeText(MyOrderActivity.this, "您选择了第"+i+"个页卡",
			// Toast.LENGTH_SHORT).show();
		}
	}

	// 平移动画
	public void tranSlate(int currentItem, long durationMillis) {
		float one = offset ;
		Log.v("offest", ""+offset);
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", ""+currIndex * one);
		Log.v("currentItem", ""+currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
		image.startAnimation(animation);// 是用ImageView来显示动画的
	}

	// 设置顶部条目颜色的变化
	public void setTextColor(int i) {
		switch (i) {
		case 1:
			view1.setTextColor(getResources().getColor(R.color.orange));
			view2.setTextColor(getResources().getColor(R.color.black));
			view3.setTextColor(getResources().getColor(R.color.black));
			view4.setTextColor(getResources().getColor(R.color.black));
			break;
		case 2:
			view1.setTextColor(getResources().getColor(R.color.black));
			view2.setTextColor(getResources().getColor(R.color.orange));
			view3.setTextColor(getResources().getColor(R.color.black));
			view4.setTextColor(getResources().getColor(R.color.black));
			break;
		case 3:
			view1.setTextColor(getResources().getColor(R.color.black));
			view2.setTextColor(getResources().getColor(R.color.black));
			view3.setTextColor(getResources().getColor(R.color.orange));
			view4.setTextColor(getResources().getColor(R.color.black));
			break;
		case 4:
			view1.setTextColor(getResources().getColor(R.color.black));
			view2.setTextColor(getResources().getColor(R.color.black));
			view3.setTextColor(getResources().getColor(R.color.black));
			view4.setTextColor(getResources().getColor(R.color.orange));
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
